package com.fh.fruit.controller.order;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.fh.controller.base.BaseController;
import com.fh.entity.Page;
import com.fh.fruit.service.order.BusinessOrdersManager;
import com.fh.util.Jurisdiction;
import com.fh.util.ObjectExcelView;
import com.fh.util.PageData;

@Controller
@RequestMapping(value = "/businessOrders")
public class BusinessOrdersController extends BaseController {

	String menuUrl = "businessOrders/listBusinessOrders.do"; //菜单地址(权限用)
	
	@Resource(name = "businessOrdersService")
	private BusinessOrdersManager businessOrdersService;

	@RequestMapping(value = "/listBusinessOrders")
	public ModelAndView listBusinessOrders(Page page) throws Exception {
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
//		String num=pd.getString("num");
//		if(num != null){
//			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
//			Date date = sdf.parse(DateUtil.getTime());
//			pd.put("LongTerm",sdf.format(date));
//		}
//		page.setShowCount(15);
		page.setPd(pd);
		List<Map<String,Object>> businessOrdersList = businessOrdersService.listBusinessOrders(page);
		List<Map<String, Object>> businessOrdersCount = businessOrdersService.businessOrdersCount(page);
		BigDecimal count = new BigDecimal("0");
		if(businessOrdersCount.isEmpty() || businessOrdersCount == null){
				count = new BigDecimal("0");
		}else{
			for(int i = 0;i<businessOrdersCount.size();i++){
				if(businessOrdersCount.get(i) == null){
					count = new BigDecimal("0");
				}else{
					Map<String, Object> bOrder = businessOrdersCount.get(i);
					count = (BigDecimal) bOrder.get("orderPrice");
				}
			}
		}
		mv.addObject("orderPrice", count);
		mv.setViewName("farm/order/businessOrders_list");
		mv.addObject("businessOrdersList", businessOrdersList);
		mv.addObject("pd", pd);
		mv.addObject("QX",Jurisdiction.getHC());	//按钮权限
		return mv;
	}
	/**导出账单信息到EXCEL
	 * @return
	 */
	@RequestMapping(value="/excel")
	public ModelAndView exportExcel(Page page){
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		try{
			if(Jurisdiction.buttonJurisdiction(menuUrl, "cha")){
				
				Map<String,Object> dataMap = new HashMap<String,Object>();
				List<String> titles = new ArrayList<String>();
				titles.add("序号"); 		//1
				titles.add("订单号");  		//2
				titles.add("用户");			//3
				titles.add("下单时间");			//4
				titles.add("商品种类");			//5
				titles.add("商品名称");			//6
				titles.add("数量");			//7
				titles.add("单位");		//8
				titles.add("单价");	//9
				titles.add("订单金额");//10
				titles.add("订单状态");//11
				titles.add("支付方式");//12
				dataMap.put("titles", titles);
				page.setShowCount(15);
				page.setCurrentPage(Integer.parseInt(pd.getString("pageNum")));
				page.setPd(pd);
				List<Map<String,Object>> businessOrdersList = businessOrdersService.listBusinessOrdersExcl(page);
				List<PageData> varList = new ArrayList<PageData>();
				for(int i=0;i<businessOrdersList.size();i++){
					PageData vpd = new PageData();
					String pay_mode=(String)businessOrdersList.get(i).get("pay_mode");
					String state=(String)businessOrdersList.get(i).get("state");
					if(pay_mode != null && !"".equals(pay_mode)){
						if("wechat".equals(pay_mode)){
							pay_mode="微信";
						}else if("alipay".equals(pay_mode)){
							pay_mode="支付宝";
						}else{
							pay_mode="余额";
						}
					}else{
						pay_mode="/";
					}
					if(state != null && !"".equals(state)){
						if("00".equals(state)){
							state="未付款";
						}else if("01".equals(state)){
							state="已付款";
						}else if("05".equals(state)){
							state="已取消";
						}
					}
					vpd.put("var1", (i+1));		//1
					vpd.put("var2", businessOrdersList.get(i).get("code").toString());		//2
					vpd.put("var3", (String)businessOrdersList.get(i).get("phone"));			//3
					vpd.put("var4", (String)businessOrdersList.get(i).get("create_time"));	//4
					vpd.put("var5", (String)businessOrdersList.get(i).get("type"));	//5
					vpd.put("var6", businessOrdersList.get(i).get("title").toString());		//6
					vpd.put("var7", businessOrdersList.get(i).get("num").toString());		//7
					vpd.put("var8", businessOrdersList.get(i).get("unit").toString());	//8
					vpd.put("var9", businessOrdersList.get(i).get("price").toString());			//9
					vpd.put("var10", businessOrdersList.get(i).get("total_price").toString());			//10
					vpd.put("var11", state);			//11
					vpd.put("var12", pay_mode);			//12
					varList.add(vpd);
				}
				dataMap.put("varList", varList);
				ObjectExcelView erv = new ObjectExcelView();					//执行excel操作
				
				mv = new ModelAndView(erv,dataMap);
			}
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}
}
